/*
 * Copyright (c) 2013, EWELL Techno. All rights reserved.
 */
package p.ithorns.framework.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

import java.io.*;
import java.util.Properties;

/**
 * <p>
 * 资源文件处理工具类
 * </p>
 *
 * @author ithorns
 */
public class PropertiesUtil {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static final String DEFAULT_ENCODING = "UTF-8";

    private static PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();

    private static ResourceLoader resourceLoader = new DefaultResourceLoader();

    /**
     * <p>
     * 加载资源文件
     * </p>
     *
     * @param resourcesPaths 资源文件路径集
     * @return 属性集
     */
    public static Properties loadProperties(String... resourcesPaths) {
        Properties props = new Properties();

        for (String location : resourcesPaths) {
            InputStream is = null;

            try {
                Resource resource = resourceLoader.getResource(location);
                is = resource.getInputStream();
                propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
            } catch (IOException e) {
                logger.error("Could not load properties from classpath:" + location + ": " + e.getMessage(), e);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            }
        }

        return props;
    }

    /**
     * <p>
     * 获取值
     * </p>
     *
     * @param filePath 资源文件路径
     * @param key      属性键
     * @return 属性值
     */
    public static String readValue(String filePath, String key) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            return props.getProperty(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * <p>
     * 写属性值
     * </p>
     *
     * @param filePath       资源文件路径
     * @param parameterName  属性参数名称
     * @param parameterValue 属性值
     */
    public static void writeProperties(String filePath, String parameterName, String parameterValue) {
        Properties props = new Properties();
        try {
            InputStream fis = new FileInputStream(filePath);
            props.load(fis);
            OutputStream fos = new FileOutputStream(filePath);
            props.setProperty(parameterName, parameterValue);
            props.store(fos, "Update '" + parameterName + "' value");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}